package com.artron.mediaartron.ui.widget;

import android.content.Context;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewConfiguration;

import me.kaelaela.verticalviewpager.VerticalViewPager;

/**
 * Class description here
 *
 * @author doing
 * @version 1.0.0
 * @since 2017-09-22.
 */

public class CustomVerticalViewPager extends VerticalViewPager {

    public CustomVerticalViewPager(Context context) {
        this(context, null);
    }

    public CustomVerticalViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    private float mMoveX;
    private float mMoveY;

    private MotionEvent mDownEvent;

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        super.onInterceptTouchEvent(event);
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mMoveX = 0;
                mMoveY = 0;
                mDownEvent = copyEvent(event);
                return false;
            case MotionEvent.ACTION_MOVE:
                if (mMoveX == 0 && mMoveY == 0) {
                    mMoveX = event.getX();
                    mMoveY = event.getY();
                    return false;
                } else {
                    int scaledTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
                    return mMoveX - event.getX() > scaledTouchSlop / 4 ||
                            mMoveY - event.getY() > scaledTouchSlop / 4;
                }
        }
        return false;
    }

    private MotionEvent copyEvent(MotionEvent event) {
        return MotionEvent.obtain(event);
    }

    public void onTouchEvent() {
        if (mDownEvent != null) {
            onTouchEvent(mDownEvent);
            mDownEvent = null;
        }
    }

    @Override
    public void onRestoreInstanceState(Parcelable state) {
        SavedState saveState = (SavedState) onSaveInstanceState();
        super.onRestoreInstanceState(saveState);
    }
}
